using BunnyEater.Common.ResponseOutput;
using BunnyEater.Domain.Base;
using BunnyEater.Service.SystemManagementService.PermissionsService;
using BunnyEater.Service.SystemManagementService.PermissionsService.Dto;
using BunnyEater.Service.SystemManagementService.UserService;
using BunnyEater.Service.SystemManagementService.UserService.Dto;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using System;

namespace BunnyEater.WebAPI.Controllers.SysManage
{
    /// <summary>
    /// 权限管理控制器，提供权限的增删改查及分页等接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = "SYSTEM")]
    [ApiController]
    public class PermissionsController : ControllerBase
    {
        private readonly IPermissionsService _permissionsService;

        /// <summary>
        /// 构造函数
        /// </summary>
        public PermissionsController(IPermissionsService permissionsService)
        {
            _permissionsService = permissionsService;
        }

        /// <summary>
        /// 分页查询权限列表
        /// </summary>
        [HttpGet]
        public async Task<ResponseReturnsEntity<Page<PermissionsListDto>>> GetPagedPermissions([FromQuery] PermissionsPagedQueryDto query)
        {
            return await _permissionsService.GetPagedPermissionsAsync(query);
        }

        /// <summary>
        /// 根据ID获取权限
        /// </summary>
        [HttpGet]
        public async Task<ResponseReturnsEntity<PermissionsListDto>> GetPermissionsById(string id)
        {
            return await _permissionsService.GetPermissionsById(id);
        }

        /// <summary>
        /// 新增权限
        /// </summary>
        [HttpPost]
        public async Task<ResponseReturnsEntity<bool>> AddPermissions(PermissionsListDto permissionsDto)
        {
            return await _permissionsService.AddPermissions(permissionsDto);
        }

        /// <summary>
        /// 修改权限
        /// </summary>
        [HttpPost]
        public async Task<ResponseReturnsEntity<bool>> UptPermissions(PermissionsListDto permissionsDto)
        {
            return await _permissionsService.UptPermissions(permissionsDto);
        }

        /// <summary>
        /// 删除权限（软删除）
        /// </summary>
        [HttpPost]
        public async Task<ResponseReturnsEntity<bool>> DeletePermissions(string id)
        {
            return await _permissionsService.DeletePermissions(id);
        }

        /// <summary>
        /// 修改权限状态
        /// </summary>
        [HttpPost]
        public async Task<ResponseReturnsEntity<bool>> UpdatePermissionsState(string id, bool state)
        {
            return await _permissionsService.UpdatePermissionsState(id, state);
        }

        /// <summary>
        /// 五表联查：获取用户及其角色和权限信息
        /// </summary>
        /// <param name="usersService">用户服务</param>
        /// <param name="userId">用户Id</param>
        /// <returns>用户角色权限信息</returns>
        [HttpGet]
        public async Task<ResponseReturnsEntity<UserRolePermissionDto>> GetUserRolePermissions([FromServices] IUsersService usersService, string userId)
        {
            try
            {
                var result = await usersService.GetUserRolePermissions(userId);
                return ResponseReturnsEntity<UserRolePermissionDto>.Success(result);
            }
            catch (InvalidOperationException ex)
            {
                return ResponseReturnsEntity<UserRolePermissionDto>.Fail(ex.Message);
            }
            catch (Exception ex)
            {
                return ResponseReturnsEntity<UserRolePermissionDto>.Fail($"获取用户角色权限信息失败: {ex.Message}");
            }
        }
    }
}